package analysis

import (
	"encoding/json"
)

func (dev *DevInfoTable) Interval_Collect_Report() {
	if MQTT_CONNECT == 0 {
		return
	}
	dev.Interval_Coil_Show()
	dev.Interval_DiscreteInputs_show()
	dev.Interval_InputReg_show()
	dev.Interval_HoldingReg_show()
}

func (dev *DevInfoTable) Interval_Coil_Show() {
	msg := ReportDataMsg{}
	for _, register := range dev.RegTable.CoisReg {
		//fmt.Println(dev.DevInfo.DevName + ":")
		//fmt.Println(register.RegName, register.Key, dev.RegBufTable.Coils[register.RegAddr])
		val := DataVal{}
		val.Key = register.Key
		val.Val = float32(dev.RegBufTable.Coils[register.RegAddr])
		val.Name = register.RegName
		val.RegAddr = register.RegAddr
		msg.Data = append(msg.Data, val)
	}
	if len(msg.Data) > 0 {
		msg.ParaType = "yk"
		msg.DevName = dev.DevInfo.DevName
		msg.DevType = dev.DevInfo.DevType
		msg.TimeStamp = dev.RegBufStateTable.Timestamp
		marshal, _ := json.Marshal(msg)
		if dev.DevInfo.MqttClient != nil {
			//fmt.Println(string(marshal))
			dev.RegBufStateTable.LockData.Lock()
			mqtt_publish_msg(dev.DevInfo.MqttClient, marshal)
			dev.RegBufStateTable.LockData.Unlock()

		}
	}
}
func (dev *DevInfoTable) Interval_DiscreteInputs_show() {
	msg := ReportDataMsg{}
	for _, register := range dev.RegTable.DiscretInput {

		//fmt.Println(dev.DevInfo.DevName + ":")
		//fmt.Println(register.RegName, register.Key, dev.RegBufTable.DiscreteInputs[register.RegAddr])
		val := DataVal{}
		val.Key = register.Key
		val.Val = float32(dev.RegBufTable.DiscreteInputs[register.RegAddr])
		//val.TimeStamp = time.Now().Format(time.DateTime)
		val.Name = register.RegName
		val.RegAddr = register.RegAddr
		msg.Data = append(msg.Data, val)
		dev.RegBufStateTable.LockData.Lock()
		dev.RegBufStateTable.DiscreteState[register.RegAddr] = 0
		dev.RegBufStateTable.LockData.Unlock()
	}
	if len(msg.Data) > 0 {
		msg.DevName = dev.DevInfo.DevName
		msg.DevType = dev.DevInfo.DevType
		msg.TimeStamp = dev.RegBufStateTable.Timestamp
		msg.ParaType = "yx"
		marshal, _ := json.Marshal(msg)
		if dev.DevInfo.MqttClient != nil {
			//fmt.Println(string(marshal))
			dev.RegBufStateTable.LockData.Lock()
			//dev.DevInfo.MqttClient.Publish("/dev/report/colloc/data", 0, false, marshal)
			mqtt_publish_msg(dev.DevInfo.MqttClient, marshal)
			dev.RegBufStateTable.LockData.Unlock()
		}
	}
}

func (dev *DevInfoTable) Interval_InputReg_show() {
	msg := ReportDataMsg{}
	for _, register := range dev.RegTable.InputReg {
		register.inputReportData(&msg, dev.RegBufTable.InputRegisters[register.RegAddr], dev.RegBufTable.InputRegisters)
		if register.ListTable != nil {
			for _, devRegister := range register.ListTable {
				devRegister.inputReportData(&msg, dev.RegBufTable.InputRegisters[register.RegAddr], dev.RegBufTable.InputRegisters)
			}
		}
	}
	if len(msg.Data) > 0 {
		msg.DevName = dev.DevInfo.DevName
		msg.DevType = dev.DevInfo.DevType
		msg.TimeStamp = dev.RegBufStateTable.Timestamp
		msg.ParaType = "yc"
		marshal, _ := json.Marshal(msg)
		if dev.DevInfo.MqttClient != nil {
			//fmt.Println(string(marshal))
			dev.RegBufStateTable.LockData.Lock()
			//dev.DevInfo.MqttClient.Publish("/dev/report/colloc/data", 0, false, marshal)
			mqtt_publish_msg(dev.DevInfo.MqttClient, marshal)
			dev.RegBufStateTable.LockData.Unlock()
		}
	}
}

func (dev *DevInfoTable) Interval_HoldingReg_show() {
	msg := ReportDataMsg{}
	for _, register := range dev.RegTable.HoldingReg {
		register.inputReportData(&msg, dev.RegBufTable.HoldingRegisters[register.RegAddr], dev.RegBufTable.HoldingRegisters)
		if register.ListTable != nil {
			for _, devRegister := range register.ListTable {
				devRegister.inputReportData(&msg, dev.RegBufTable.HoldingRegisters[register.RegAddr], dev.RegBufTable.HoldingRegisters)
			}
		}
	}
	//fmt.Println(dev.DevInfo.DevName + ":")
	//fmt.Println(dev.RegBufTable.HoldingRegisters[register.RegAddr], register.Ratio)
	//var float float64
	//switch register.DataType {
	//case "INT16":
	//	float, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", float32(int16(dev.RegBufTable.HoldingRegisters[register.RegAddr]))*register.Ratio), 64)
	//	break
	//case "UINT16":
	//	float, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", float32(dev.RegBufTable.HoldingRegisters[register.RegAddr])*register.Ratio), 64)
	//	break
	//}
	////float, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", float64(dev.RegBufTable.HoldingRegisters[register.RegAddr])*register.Ratio), 64)
	//
	////fmt.Println(register.RegName, register.Key, float, register.Unit)
	//val := DataVal{}
	//val.Key = register.Key
	//val.Val = float32(float)
	////val.TimeStamp = time.Now().Format(time.DateTime)
	//val.Name = register.RegName
	//val.RegAddr = register.RegAddr
	//msg.Data = append(msg.Data, val)
	//fmt.Println(register.RegName, register.Key, float64(dev.RegBufTable.HoldingRegisters[register.RegAddr])*register.Ratio, register.Unit)
	if len(msg.Data) > 0 {
		msg.DevName = dev.DevInfo.DevName
		msg.DevType = dev.DevInfo.DevType
		msg.TimeStamp = dev.RegBufStateTable.Timestamp
		msg.ParaType = "yt"
		marshal, _ := json.Marshal(msg)

		if dev.DevInfo.MqttClient != nil {
			//fmt.Println(string(marshal))
			//dev.DevInfo.MqttClient.Publish("/dev/report/colloc/data", 0, false, marshal)
			dev.RegBufStateTable.LockData.Lock()
			mqtt_publish_msg(dev.DevInfo.MqttClient, marshal)
			dev.RegBufStateTable.LockData.Unlock()
		}
	}
}
